<template>
  <div>
    <el-divider content-position="left">业绩排行榜</el-divider>
    <div class="demo-input-suffix gl_searchBox">
      <el-row :gutter="20">
        <el-col :md="6">
          <p>时间筛选</p>
          <el-date-picker
            v-model="chooseTime"
            size="mini"
            style="width: 100%;"
            type="daterange"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
            @change="handleTimeChange"
          />
        </el-col>
        <el-col :md="14" class="search_btn">
          <!-- 查询按钮 -->
          <el-button type="primary" size="mini" @click="getPageList()">筛选</el-button>
          <!-- 重置按钮 -->
          <el-button type="warning" size="mini" @click="clickReset()">{{ $t('button.Reset') }}</el-button>
        </el-col>
      </el-row>
    </div>

    <el-table v-loading="tableLoading" :data="tableData" class="gl_table_noBorder gl_mt_15" size="mini" style="width: 100%;">
      <el-table-column label="排名" type="index" width="70" />
      <el-table-column label="业务员" prop="employeeName" />
      <el-table-column label="成交金额（元）">
        <template slot-scope="{ row }">
          {{ row.amount ? formatMoney(row.amount) : '' }}
        </template>
      </el-table-column>
      <el-table-column prop="orderCount" label="成交订单数" />
      <el-table-column :label="$t('table.actions')" width="100">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" @click="clickDetail(row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row class="gl_pt_15" style="text-align: right;">
      <el-pagination
        :current-page="qo.Page"
        :page-size="qo.MaxResultCount"
        :total="total"
        layout="total,sizes,prev,pager,next,jumper"
        :page-sizes="[10, 20, 30, 40]"
        @size-change="sizeChange"
        @current-change="pageIndexChange"
      />
    </el-row>
  </div>
</template>

<script>
import { Api_GET_ClubSalesCardStatisticsEmployeeSalesAmountsQO } from '@/lib/form/hall/swaggerForm';
import { api_GET_ClubSalesCardStatisticsEmployeeSalesAmounts } from '@/extend/api/hallswaggerApi/app';
export default {
  data() {
    return {
      chooseTime: [],
      qo: new Api_GET_ClubSalesCardStatisticsEmployeeSalesAmountsQO(),
      tableLoading: false,
      total: 0,
      tableData: [],
      pickerOptions: {
        shortcuts: [{
          text: '最近一天',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }]
      }
    };
  },
  created() {
    // const day1 = this.handleDate(-30, '-');
    // const day2 = this.handleDate(0, '-');
    // this.chooseTime = [day1, day2];
    // this.qo.StartDate = day1;
    // this.qo.EndDate = day2;
    this.getPageList();
  },
  methods: {
    // 获取课程列表
    getPageList(page) {
      this.qo.Page = page || 1;
      this.qo.SkipCount = (this.qo.Page - 1) * this.qo.MaxResultCount;
      this.tableLoading = true;
      api_GET_ClubSalesCardStatisticsEmployeeSalesAmounts(this.qo).then(res => {
        this.tableLoading = false;
        this.tableData = res.items;
        this.total = res.totalCount;
      }, error => {
        this.tableLoading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    clickDetail(item) {
      this.$router.push({ name: 'EnrollmentAnalysis', query: { type: 'analyse', id: item.employeeId }});
    },
    clickReset() {
      this.qo = new Api_GET_ClubSalesCardStatisticsEmployeeSalesAmountsQO();
      // const day1 = this.handleDate(-30, '-');
      // const day2 = this.handleDate(0, '-');
      // this.chooseTime = [day1, day2];
      // this.qo.StartDate = day1;
      // this.qo.EndDate = day2;
      this.chooseTime = [];
      this.getPageList();
    },
    handleTimeChange(time) {
      this.qo.StartDate = time ? time[0] + ' 00:00:00' : null;
      this.qo.EndDate = time ? time[1] + ' 23:59:59' : null;
      // this.qo.StartDate = time ? time[0] : null;
      // this.qo.EndDate = time ? time[1] : null;
      this.getPageList();
    },
    // 处理初始的统计时间
    handleDate (num, str) {
      var today = new Date();
      var nowTime = today.getTime();
      var ms = 24 * 3600 * 1000 * num;
      today.setTime(parseInt(nowTime + ms));
      var oYear = today.getFullYear();
      var oMoth = (today.getMonth() + 1).toString();
      if (oMoth.length <= 1) oMoth = '0' + oMoth;
      var oDay = today.getDate().toString();
      if (oDay.length <= 1) oDay = '0' + oDay;
      return oYear + str + oMoth + str + oDay;
    },
    // 格式化货币格式 （分）
    formatMoney(num) {
      if (typeof num === 'number' && !Number.isNaN(num)) {
        // num 是数字类型 且不是 NaN
        let strNum = String(num); // 金额文本类型
        if (num >= 0) {
          strNum = strNum.padStart(3, '0'); // 用0填充保证最小三位数
          return strNum.slice(0, -2) + '.' + strNum.slice(-2); // 金额拼接
        } else {
          const _mark = strNum.slice(0, 1); // 负号
          const _noMark = strNum.slice(1); // 无负号数值
          strNum = _noMark.padStart(3, '0'); // 用0填充保证最小三位数
          return _mark + strNum.slice(0, -2) + '.' + strNum.slice(-2); // 金额拼接
        }
      } else {
        return '';
      }
    },
    // 分页
    sizeChange(MaxResultCount) {
      this.qo.MaxResultCount = MaxResultCount;
      this.getPageList();
    },
    pageIndexChange(currentPage) {
      this.getPageList(currentPage);
    }
  }
};
</script>
<style lang="scss" scoped>
</style>
